import { EDoc, EDocTree } from "@/views/type";
import { defineStore } from "pinia";
import { ref } from "vue";

import { useApi } from "@/api";
import { Tool } from "@/utils/tool";
const api = useApi();

const useDoc =defineStore('doc',()=>{

    const doc =ref<EDoc>()
    const docS = ref<EDoc[]>([])
    const docTree = ref<EDocTree[]>([])

    async function getDocs(id:number){
        const data = await api.getSelectDocAll<EDoc[]>(id)
        docS.value = data
        doc.value = Tool.copy(data[0])

        docTree.value =Tool.array2Tree(data,0)
    } 
    
    function addDoc(doc:EDoc){
        doc.voteCount ++;
        doc.viewCount ++;
    }
    
    


    return {doc,docS,docTree,getDocs,addDoc}
},{persist:true})


export default useDoc